/*
 * WorkTimerGUI.java
 *
 * Created on July 13, 2006, 9:27 AM
 */

package org.vcboard.view;

import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import org.apache.log4j.*;
import org.vcboard.*;
import org.vcboard.database.*;
import org.vcboard.event.*;
import org.vcboard.model.*;
import org.vcboard.view.model.*;
import org.vcboard.view.renderer.LogsTableCellRenderer;

/**
 *
 * @author  Josh
 */
public class WorkTimerGUI
      extends javax.swing.JFrame
      implements UserChangedListener
{
   private Logger log = Logger.getLogger(WorkTimerGUI.class);
   private static WorkTimerGUI workTimerGUI = null;
   
   private static final Color RED = new Color(203, 37, 37);
   private static final Color GREEN = new Color(40, 175, 40);
   
   private static Timer updateTimer = null;
   
   private static UnauthorizedTableModel unauthTableModel = new UnauthorizedTableModel();
   private static LogsTableModel logsTableModel = new LogsTableModel();
   
   /** Creates new form WorkTimerGUI */
   public WorkTimerGUI()
   {
      initComponents();
      
      WorkTimer.addListener(this);
      WorkTimer.centerForm(this);
      
      toggleOptions();
      currentLabel.setVisible(false);
      currentTimeLabel.setVisible(false);
      
      logsTable.setSelectionMode(0);
      logsTable.getTableHeader().setReorderingAllowed(false);
      logsTable.setModel(unauthTableModel);
      logsTable.setDefaultRenderer(Object.class, new LogsTableCellRenderer());
      currentUserLabel.setForeground(RED);
      
      WorkTimer.fireUserChanged();
      
      updateTimer = new Timer(1000, new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
            timerUpdate();
         }
      });
   }
   
   public static WorkTimerGUI getInstance()
   {
      if (workTimerGUI == null)
      {
         workTimerGUI = new WorkTimerGUI();
      }
      
      return workTimerGUI;
   }
   
   private void toggleStatus()
   {
      if (WorkTimer.getUser().getUserId() != -1)
      {
         toggleStatusButton.setEnabled(true);
         toggleStatusMenuItem.setEnabled(true);
         
         if (MySQLDbIf.getInstance().isClockedIn())
         {
            toggleStatusButton.setText("Clock Out");
            toggleStatusButton.setForeground(RED);
            toggleStatusButton.setSelected(true);
            
            toggleStatusMenuItem.setText("Clock Out");
            toggleStatusMenuItem.setForeground(RED);
         }
         else
         {
            toggleStatusButton.setText("Clock In");
            toggleStatusButton.setForeground(GREEN);
            toggleStatusButton.setSelected(false);
            
            toggleStatusMenuItem.setText("Clock In");
            toggleStatusMenuItem.setForeground(GREEN);
         }
      }
      else
      {
         toggleStatusButton.setEnabled(false);
         toggleStatusButton.setText("Clock In");
         toggleStatusButton.setSelected(false);
         toggleStatusMenuItem.setEnabled(false);
      }
   }
   
   /**
    * Toggles buttons/menu items according to what is selected in the table
    */
   public void toggleOptions()
   {
      if (WorkTimer.getUser().getUserId() != -1)
      {
         modifySelectedLogButton.setEnabled(true);
         modifySelectedLogMenuItem.setEnabled(true);
      }
      else
      {
         modifySelectedLogButton.setEnabled(false);
         modifySelectedLogMenuItem.setEnabled(false);
      }
      
      toggleStatus();
   }
   
   public void showModifyLog()
   {
      if (logsTable.getSelectedRow() != -1)
      {
         // Show the modify log form
         log.info("Modifying log " + WorkTimer.getLogs().get(logsTable.getSelectedRow()).getLogId());
         
         WorkTimer.getModifyLog().setLog(WorkTimer.getLogs().get(logsTable.getSelectedRow()));
         WorkTimer.getModifyLog().setVisible(true);
      }
      else
      {
         // We should never get here, but catch it just in case
         JOptionPane.showMessageDialog(null,
               "You must select a log to modify!",
               "Modify Log Error",
               JOptionPane.ERROR_MESSAGE);
         WorkTimer.getModifyLog().setVisible(false);
      }
   }
   
   private void confirmToggleStatus()
   {
      if (!toggleStatusButton.isSelected())
      {
         int response = JOptionPane.showConfirmDialog(null,
               "Are you sure?",
               "Confirmation",
               JOptionPane.YES_NO_OPTION,
               JOptionPane.QUESTION_MESSAGE);
         
         if (response == JOptionPane.OK_OPTION)
         {
            MySQLDbIf.getInstance().toggleStatus();
         }
      }
      else
      {
         MySQLDbIf.getInstance().toggleStatus();
      }
      toggleStatus();
   }
   
   /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
   // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
   private void initComponents()
   {
      logsScrollPane = new javax.swing.JScrollPane();
      logsTable = new javax.swing.JTable();
      toggleStatusButton = new javax.swing.JToggleButton();
      modifySelectedLogButton = new javax.swing.JButton();
      jSeparator1 = new javax.swing.JSeparator();
      currentUserLabel = new javax.swing.JLabel();
      jLabel1 = new javax.swing.JLabel();
      periodStartCombo = new javax.swing.JComboBox();
      jLabel2 = new javax.swing.JLabel();
      totalTimeLabel = new javax.swing.JLabel();
      currentLabel = new javax.swing.JLabel();
      currentTimeLabel = new javax.swing.JLabel();
      menuBar = new javax.swing.JMenuBar();
      actionsMenu = new javax.swing.JMenu();
      loginMenuItem = new javax.swing.JMenuItem();
      logoutMenuItem = new javax.swing.JMenuItem();
      toggleStatusMenuItem = new javax.swing.JMenuItem();
      modifySelectedLogMenuItem = new javax.swing.JMenuItem();
      jSeparator2 = new javax.swing.JSeparator();
      exitMenuItem = new javax.swing.JMenuItem();
      toolsMenu = new javax.swing.JMenu();
      dbConnInfoMenuItem = new javax.swing.JMenuItem();
      setPeriodMenuItem = new javax.swing.JMenuItem();
      changePassMenuItem = new javax.swing.JMenuItem();
      newUserMenuItem = new javax.swing.JMenuItem();
      helpMenu = new javax.swing.JMenu();
      aboutMenuItem = new javax.swing.JMenuItem();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      setTitle("Work Timer");
      setResizable(false);
      logsTable.setModel(new javax.swing.table.DefaultTableModel(
         new Object [][]
         {
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null}
         },
         new String []
         {
            "Title 1", "Title 2", "Title 3", "Title 4"
         }
      ));
      logsTable.addMouseListener(new java.awt.event.MouseAdapter()
      {
         public void mouseClicked(java.awt.event.MouseEvent evt)
         {
            logsTableMouseClicked(evt);
         }
      });

      logsScrollPane.setViewportView(logsTable);

      toggleStatusButton.setMnemonic('c');
      toggleStatusButton.setText("Clock In/Out");
      toggleStatusButton.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            toggleStatusButtonActionPerformed(evt);
         }
      });

      modifySelectedLogButton.setMnemonic('m');
      modifySelectedLogButton.setText("Modify Log");
      modifySelectedLogButton.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            modifySelectedLogButtonActionPerformed(evt);
         }
      });

      currentUserLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
      currentUserLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      currentUserLabel.setText("You are not logged in.");

      jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
      jLabel1.setText("Period:");

      periodStartCombo.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            periodStartComboActionPerformed(evt);
         }
      });

      jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
      jLabel2.setText("Total Time:");

      totalTimeLabel.setText("00:00");

      currentLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
      currentLabel.setText("Current Total:");

      currentTimeLabel.setText("00:00");

      actionsMenu.setMnemonic('a');
      actionsMenu.setText("Actions");
      loginMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
      loginMenuItem.setMnemonic('l');
      loginMenuItem.setText("Login...");
      loginMenuItem.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            loginMenuItemActionPerformed(evt);
         }
      });

      actionsMenu.add(loginMenuItem);

      logoutMenuItem.setText("Logout...");
      logoutMenuItem.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            logoutMenuItemActionPerformed(evt);
         }
      });

      actionsMenu.add(logoutMenuItem);

      toggleStatusMenuItem.setText("Clock In/Out");
      toggleStatusMenuItem.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            toggleStatusMenuItemActionPerformed(evt);
         }
      });

      actionsMenu.add(toggleStatusMenuItem);

      modifySelectedLogMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.CTRL_MASK));
      modifySelectedLogMenuItem.setMnemonic('m');
      modifySelectedLogMenuItem.setText("Modify Selected Log");
      modifySelectedLogMenuItem.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            modifySelectedLogMenuItemActionPerformed(evt);
         }
      });

      actionsMenu.add(modifySelectedLogMenuItem);

      actionsMenu.add(jSeparator2);

      exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
      exitMenuItem.setMnemonic('x');
      exitMenuItem.setText("Exit");
      exitMenuItem.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            exitMenuItemActionPerformed(evt);
         }
      });

      actionsMenu.add(exitMenuItem);

      menuBar.add(actionsMenu);

      toolsMenu.setMnemonic('t');
      toolsMenu.setText("Tools");
      dbConnInfoMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
      dbConnInfoMenuItem.setMnemonic('s');
      dbConnInfoMenuItem.setText("Settings...");
      dbConnInfoMenuItem.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            dbConnInfoMenuItemActionPerformed(evt);
         }
      });

      toolsMenu.add(dbConnInfoMenuItem);

      setPeriodMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
      setPeriodMenuItem.setMnemonic('p');
      setPeriodMenuItem.setText("Set Period");
      setPeriodMenuItem.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            setPeriodMenuItemActionPerformed(evt);
         }
      });

      toolsMenu.add(setPeriodMenuItem);

      changePassMenuItem.setText("Change Password...");
      changePassMenuItem.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            changePassMenuItemActionPerformed(evt);
         }
      });

      toolsMenu.add(changePassMenuItem);

      newUserMenuItem.setText("Create a new user...");
      newUserMenuItem.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            newUserMenuItemActionPerformed(evt);
         }
      });

      toolsMenu.add(newUserMenuItem);

      menuBar.add(toolsMenu);

      helpMenu.setMnemonic('h');
      helpMenu.setText("Help");
      aboutMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.CTRL_MASK));
      aboutMenuItem.setMnemonic('a');
      aboutMenuItem.setText("About WorkTimer");
      aboutMenuItem.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            aboutMenuItemActionPerformed(evt);
         }
      });

      helpMenu.add(aboutMenuItem);

      menuBar.add(helpMenu);

      setJMenuBar(menuBar);

      org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
         .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
               .add(org.jdesktop.layout.GroupLayout.LEADING, logsScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
               .add(org.jdesktop.layout.GroupLayout.LEADING, jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
               .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                  .add(toggleStatusButton)
                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                  .add(currentUserLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                  .add(modifySelectedLogButton))
               .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                  .add(jLabel1)
                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                  .add(periodStartCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 97, Short.MAX_VALUE)
                  .add(currentLabel)
                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                  .add(currentTimeLabel)
                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                  .add(jLabel2)
                  .add(2, 2, 2)
                  .add(totalTimeLabel)))
            .addContainerGap())
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
         .add(layout.createSequentialGroup()
            .addContainerGap()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
               .add(toggleStatusButton)
               .add(modifySelectedLogButton)
               .add(currentUserLabel))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(logsScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
               .add(periodStartCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(jLabel1)
               .add(jLabel2)
               .add(totalTimeLabel)
               .add(currentTimeLabel)
               .add(currentLabel))
            .addContainerGap())
      );
      pack();
   }// </editor-fold>//GEN-END:initComponents

   private void newUserMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_newUserMenuItemActionPerformed
   {//GEN-HEADEREND:event_newUserMenuItemActionPerformed
      NewUser newUser = new NewUser();
      newUser.setVisible(true);
   }//GEN-LAST:event_newUserMenuItemActionPerformed

   private void changePassMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_changePassMenuItemActionPerformed
   {//GEN-HEADEREND:event_changePassMenuItemActionPerformed
      ChangePassword changePass = new ChangePassword();
      changePass.setVisible(true);
   }//GEN-LAST:event_changePassMenuItemActionPerformed

   private void dbConnInfoMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_dbConnInfoMenuItemActionPerformed
   {//GEN-HEADEREND:event_dbConnInfoMenuItemActionPerformed
      WorkTimer.getDbInfo().setVisible(true);
   }//GEN-LAST:event_dbConnInfoMenuItemActionPerformed

   private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_aboutMenuItemActionPerformed
   {//GEN-HEADEREND:event_aboutMenuItemActionPerformed
      JOptionPane.showMessageDialog(null,
            "This application was created in an effort to keep track of how \n" +
            "long I worked on a certain job.  Perhaps you can find use for \n" +
            "this program as well.  Please direct criticisms to me at \n" +
            "wheaties.box@gmail.com\n\nWork Timer\nCopyright 2006 Josh " +
            "VanderLinden\nAll rights reserved.",
            "About",
            JOptionPane.INFORMATION_MESSAGE);
   }//GEN-LAST:event_aboutMenuItemActionPerformed

   private void periodStartComboActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_periodStartComboActionPerformed
   {//GEN-HEADEREND:event_periodStartComboActionPerformed
      if (periodStartCombo.getSelectedItem() != null)
      {
         if (periodStartCombo.getSelectedItem().getClass() == LogDate.class)
         {
            WorkTimer.setLogPeriod((LogDate) periodStartCombo.getSelectedItem());
            //WorkTimer.updateLogs();
            WorkTimer.fireLogsChanged();
         }
      }
   }//GEN-LAST:event_periodStartComboActionPerformed
   
   private void setPeriodMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_setPeriodMenuItemActionPerformed
   {//GEN-HEADEREND:event_setPeriodMenuItemActionPerformed
      WorkTimer.getSetPeriod().setModel(WorkTimer.getUser());
      WorkTimer.getSetPeriod().setVisible(true);
   }//GEN-LAST:event_setPeriodMenuItemActionPerformed
   
   private void toggleStatusMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_toggleStatusMenuItemActionPerformed
   {//GEN-HEADEREND:event_toggleStatusMenuItemActionPerformed
      toggleStatus();
      confirmToggleStatus();
   }//GEN-LAST:event_toggleStatusMenuItemActionPerformed
   
   private void toggleStatusButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_toggleStatusButtonActionPerformed
   {//GEN-HEADEREND:event_toggleStatusButtonActionPerformed
      confirmToggleStatus();
   }//GEN-LAST:event_toggleStatusButtonActionPerformed
   
   private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_exitMenuItemActionPerformed
   {//GEN-HEADEREND:event_exitMenuItemActionPerformed
      dispose();
   }//GEN-LAST:event_exitMenuItemActionPerformed
   
   private void logoutMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_logoutMenuItemActionPerformed
   {//GEN-HEADEREND:event_logoutMenuItemActionPerformed
      WorkTimer.setUser(new User());
   }//GEN-LAST:event_logoutMenuItemActionPerformed
   
   private void loginMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_loginMenuItemActionPerformed
   {//GEN-HEADEREND:event_loginMenuItemActionPerformed
      WorkTimer.authorize();
   }//GEN-LAST:event_loginMenuItemActionPerformed
   
   private void logsTableMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_logsTableMouseClicked
   {//GEN-HEADEREND:event_logsTableMouseClicked
      if (evt.getClickCount() == 2)
      {
         if (WorkTimer.getUser().getUserId() != -1)
         {
            // Attempt to modify the item
            showModifyLog();
         }
         else
         {
            // Authorize
            WorkTimer.authorize();
         }
      }
   }//GEN-LAST:event_logsTableMouseClicked
   
   private void modifySelectedLogMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_modifySelectedLogMenuItemActionPerformed
   {//GEN-HEADEREND:event_modifySelectedLogMenuItemActionPerformed
      showModifyLog();
   }//GEN-LAST:event_modifySelectedLogMenuItemActionPerformed
   
   private void modifySelectedLogButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_modifySelectedLogButtonActionPerformed
   {//GEN-HEADEREND:event_modifySelectedLogButtonActionPerformed
      showModifyLog();
   }//GEN-LAST:event_modifySelectedLogButtonActionPerformed
   
   /**
    * @param args the command line arguments
    */
   public static void main(String args[])
   {
      java.awt.EventQueue.invokeLater(new Runnable()
      {
         public void run()
         {
            new WorkTimerGUI().getInstance().setVisible(true);
         }
      });
   }
   
   public void userChanged()
   {
      if (WorkTimer.getUser().getUserId() != -1)
      {
         currentUserLabel.setText("Logged in as " + WorkTimer.getUser().getUsername());
         currentUserLabel.setForeground(GREEN);
         logsTable.setModel(logsTableModel);
         loginMenuItem.setVisible(false);
         logoutMenuItem.setVisible(true);
         
         toggleStatusMenuItem.setVisible(true);
         modifySelectedLogMenuItem.setVisible(true);
         changePassMenuItem.setVisible(true);
         setPeriodMenuItem.setVisible(true);
      
         long time = WorkTimer.getUser().getStart().getTime() / 1000;
         long freq = WorkTimer.getUser().getPeriod();
         long now = System.currentTimeMillis() / 1000;
         Vector<LogDate> dates = new Vector<LogDate>();

         periodStartCombo.removeAllItems();
         while (time + freq < now + freq)
         {
            dates.add(new LogDate(time));
            time += freq;
         }
         
         for (int i = dates.size() - 1; i >= 0; --i)
         {
            periodStartCombo.addItem(dates.get(i));
         }
         updateTimer.start();
      }
      else
      {
         currentUserLabel.setText("You are not logged in.");
         currentUserLabel.setForeground(RED);
         logsTable.setModel(unauthTableModel);
         loginMenuItem.setVisible(true);
         logoutMenuItem.setVisible(false);
         
         toggleStatusMenuItem.setVisible(false);
         modifySelectedLogMenuItem.setVisible(false);
         setPeriodMenuItem.setVisible(false);
         changePassMenuItem.setVisible(false);
         periodStartCombo.removeAllItems();
         periodStartCombo.addItem("Please login");
      }
      
      toggleOptions();
      toggleStatus();
   }
   
   public void timerUpdate()
   {
      if (WorkTimer.getUser().getUserId() != -1 && 
            WorkTimer.getTimeout() > WorkTimer.getUser().getTimeout() &&
            WorkTimer.getUser().isTimeoutEnabled())
      {
         // Logout automatically
         log.info("Automatically logging out user...");
         WorkTimer.setUser(new User());
         WorkTimer.getModifyLog().setVisible(false);
         JOptionPane.showMessageDialog(null,
               "You've been logged out automatically.",
               "Notice",
               JOptionPane.INFORMATION_MESSAGE);
      }
      
      boolean isCurrent = false;
      
      if (WorkTimer.getUser().getUserId() != -1)
      {
         Vector<TimeLog> allLogs = WorkTimer.getLogs();
         long total = 0;
         
         for (TimeLog tl : allLogs)
         {
            total += tl.getDuration().getDuration();
            
            if (tl.getClockOut() == null)
            {
               isCurrent = true;
               currentTimeLabel.setText(tl.getDuration().getTime());
            }
         }
         
         Duration tmp = new Duration(total);
         totalTimeLabel.setText(tmp.getTime());
         
         WorkTimer.addToTimeout();
         
         changePassMenuItem.setVisible(true);
      }
      else
      {
         totalTimeLabel.setText("00:00");
         changePassMenuItem.setVisible(false);
      }
      
      if (!isCurrent)
      {
         currentLabel.setVisible(false);
         currentTimeLabel.setVisible(false);
      }
      else
      {
         currentLabel.setVisible(true);
         currentTimeLabel.setVisible(true);
      }
   }
   
   public void setTooltips()
   {
      if (WorkTimer.getUser().getUserId() != -1)
      {
         // Loop through all displayed logs and create a tooltip
         int logCount = WorkTimer.getLogs().size();
         Map dates = new HashMap();
         
         for (int i = 0; i < logCount; i++)
         {
            TimeLog tmp = WorkTimer.getLogs().get(i);
            long dur = 0;
            
            if (dates.get(tmp.getDate()) != null)
            {
               dur = ((Long) dates.get(tmp.getDate())) + tmp.getDuration().getDuration();
            }
            else
            {
               dur = tmp.getDuration().getDuration();
            }
            
            dates.put(tmp.getDate(), dur);
         }
         
         //logsTable.setToolTipText()
      }
   }
   
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JMenuItem aboutMenuItem;
   private javax.swing.JMenu actionsMenu;
   private javax.swing.JMenuItem changePassMenuItem;
   private javax.swing.JLabel currentLabel;
   private javax.swing.JLabel currentTimeLabel;
   private javax.swing.JLabel currentUserLabel;
   private javax.swing.JMenuItem dbConnInfoMenuItem;
   private javax.swing.JMenuItem exitMenuItem;
   private javax.swing.JMenu helpMenu;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JSeparator jSeparator1;
   private javax.swing.JSeparator jSeparator2;
   private javax.swing.JMenuItem loginMenuItem;
   private javax.swing.JMenuItem logoutMenuItem;
   private javax.swing.JScrollPane logsScrollPane;
   private javax.swing.JTable logsTable;
   private javax.swing.JMenuBar menuBar;
   private javax.swing.JButton modifySelectedLogButton;
   private javax.swing.JMenuItem modifySelectedLogMenuItem;
   private javax.swing.JMenuItem newUserMenuItem;
   private javax.swing.JComboBox periodStartCombo;
   private javax.swing.JMenuItem setPeriodMenuItem;
   private javax.swing.JToggleButton toggleStatusButton;
   private javax.swing.JMenuItem toggleStatusMenuItem;
   private javax.swing.JMenu toolsMenu;
   private javax.swing.JLabel totalTimeLabel;
   // End of variables declaration//GEN-END:variables
   
}
